package com.jiuyun.servlet;

import com.jiuyun.dao.StudentDao;
import com.jiuyun.dao.impl.StudentDaoImpl;
import com.jiuyun.entity.Classes;
import com.jiuyun.entity.Student;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/upload")
public class UploadServlet extends HttpServlet {
    private static final int MAX_FILE_SIZE = 1024 * 1024 * 4; // 4MB(// 设置文件的最大值)
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //文件上传后的位置  服务器的运行位置
        String appPath = request.getServletContext().getRealPath("/WEB-INF/classes");
        System.out.println("运行位置"+appPath);
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        // 设置最大文件上传值 默认2M
        upload.setFileSizeMax(MAX_FILE_SIZE);
        upload.setHeaderEncoding("UTF-8");

        String fname=null;
        // 解析请求的内容提取文件数据
        try {
            List<FileItem> formItems = upload.parseRequest(request);
            for (FileItem item : formItems) {
                if(item.isFormField()){//判断是不是文件
                    //???
                    System.out.println("不是文件");
                }else {//else是文件    运行位置D:\Ij\Java\hellomoto\target\hellomoto\.
                    fname=item.getName();//iphone.jpg
                    File file= new File(appPath+"/"+fname);
                    item.write(file);// 保存文件到硬盘
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        StudentDao dao=new StudentDaoImpl();
        ClassLoader classLoader = UploadServlet.class.getClassLoader();//得到类加载
        String filename=fname;
        InputStream input=classLoader.getResourceAsStream(filename);//文件输入流
        //打开excel文件
        XSSFWorkbook sheets=new XSSFWorkbook(input);
        XSSFSheet sheet=sheets.getSheetAt(0);//获取第几张表
        //获取行数
        int rows=sheet.getPhysicalNumberOfRows();
        //获取列
        XSSFRow row=sheet.getRow(0);
        int cols=row.getPhysicalNumberOfCells();
        System.out.println("行数"+rows);
        System.out.println("列数"+cols);
        for(int i=1;i<rows;i++){
            row = sheet.getRow(i);//获取i行
            int c0 = (int)(row.getCell(0).getNumericCellValue());//读取表数据第一行(int类型)
            String c1 = row.getCell(1).toString();//读取表数据第二行(String类型)
            String c2 = row.getCell(2).toString();
//            int c3 =(int)(row.getCell(3).getNumericCellValue());
            System.out.println(c0+"--"+c1+"--"+c2+"--");

            Student s=new Student();
            s.setClassid(2);
            s.setStname(c1);
            s.setStpass(c2);
            int st = 0;
            try {
                st = dao.add(s);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            System.out.println(st);
        }


        request.setAttribute("tupian",fname);
        request.getRequestDispatcher("/WEB-INF/index.jsp").forward(request,response);
    }
}
